import path from "path"
import dayjs from "dayjs"
import prettier from "prettier"
import fs from "fs-extra"

import { type ITask } from "@/interface"

const initData = async () => {
  const envDutyPath = path.join(__dirname, "../config/env.duty.json")

  const dutyEnv = fs.readFileSync(envDutyPath, "utf8")

  const realTime = dayjs().format("YYYY-MM-DD")

  const newStr = dutyEnv.replace(/\d{4}-\d{2}-\d{2}/gi, realTime)

  const formatedStr = await prettier.format(newStr, { parser: "json" })

  fs.writeFileSync(envDutyPath, formatedStr)

  const json = <ITask>fs.readJsonSync(envDutyPath)

  return json
}

export default initData
